﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Homework12.Models
{
    public class Order
    {

        public int OrderID { set; get; }//订单编号
        public List<OrderItem> OrderItems = new List<OrderItem>();
        public Customer Customer { get; set; }
        public double TotalPrice { set; get; }
        public static int OrderQuantity = 0;//当前订单量

        public Order() { }
        public Order(Customer customer, List<OrderItem> orderItems)
        {
            OrderID = OrderQuantity;
            Customer = customer;
            OrderItems = orderItems;
            OrderQuantity += 1;//订单编号直接由订单量决定，确保订单编号不重复
            foreach (OrderItem oi in orderItems)
                TotalPrice += oi.Price;
        }



        public override bool Equals(object obj)
        {
            var order = obj as Order;
            return order != null &&
                   EqualityComparer<List<OrderItem>>.Default.Equals(OrderItems, order.OrderItems);
        }



        public override string ToString()
        {
            String str1 = "OrderNum:" + OrderID + " Customer:" + Customer + "\nOrderItems:\n";
            foreach (OrderItem orderItem in OrderItems)
            {
                str1 += orderItem.ToString();
            }
            str1 += "TotalPrice:" + TotalPrice;
            return str1;
        }

        public override int GetHashCode()
        {
            var hashCode = -996025175;
            hashCode = hashCode * -1521134295 + OrderID.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<List<OrderItem>>.Default.GetHashCode(OrderItems);
            hashCode = hashCode * -1521134295 + EqualityComparer<Customer>.Default.GetHashCode(Customer);
            hashCode = hashCode * -1521134295 + TotalPrice.GetHashCode();
            return hashCode;
        }
    }
}